package com.company.biz.purchase.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.company.biz.purchase.mapper.OrderFormDao;
import com.company.biz.purchase.service.OrderFormService;
import com.company.purchase.entity.PurchaseOrderEntity;
import com.company.purchase.req.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@Service
public class OrderFormServiceImpl extends ServiceImpl<OrderFormDao,PurchaseOrderEntity> implements OrderFormService {



    @Resource
    private OrderFormDao orderFormDao;


    /**
     * 订单分页
     * */
    @Override
    public List<PurchaseOrderEntity> getPurchaseOrderListPages(getPurchaseListPagesReq req) {


        System.err.println(req.getDistributorName());

        LambdaQueryWrapper<PurchaseOrderEntity> QueryWrapper = new QueryWrapper<PurchaseOrderEntity>()
                .lambda().eq(PurchaseOrderEntity :: getisDel,"1");
        QueryWrapper.like(StringUtils.isNotBlank(req.getPurchaseId()),
                PurchaseOrderEntity::getPurchaseId,req.getPurchaseId());
        QueryWrapper.like(StringUtils.isNotBlank(req.getDistributorName()),
                PurchaseOrderEntity::getDistributorName,req.getDistributorName());


        Page<PurchaseOrderEntity> page = page(new Page<>(req.getPage(),req.getSize()),QueryWrapper);
        if(CollUtil.isEmpty(page.getRecords())){
            System.err.println("空");
        }

        List<PurchaseOrderEntity> records = page.getRecords();

        return records;
    }

    /**
     * 更具id查询订单
     * */
    @Override
    public List<PurchaseOrderEntity> getPurchaseById(String req) {
        List<PurchaseOrderEntity> purchaseOrderEntities = new ArrayList<>();
        if(StringUtils.isNotEmpty(req)){


            LambdaQueryWrapper<PurchaseOrderEntity> QueryWrapper = new QueryWrapper<PurchaseOrderEntity>()
                    .lambda().eq(PurchaseOrderEntity :: getisDel,"1");

            QueryWrapper.eq(StringUtils.isNotBlank(req),PurchaseOrderEntity :: getPurchaseId,req);

            PurchaseOrderEntity purchaseOrderEntity = orderFormDao.selectOne(QueryWrapper);

            purchaseOrderEntities.add(purchaseOrderEntity);
        }


        return purchaseOrderEntities;

    }





    /**
     * 根据采购id删除
     * */
    @Transactional(rollbackFor=Exception.class)
    public boolean PurchaseDeletById(PurchaseDeletByIdReq req) {

        LambdaQueryWrapper<PurchaseOrderEntity> queryWrapper = new QueryWrapper<PurchaseOrderEntity>()
                .lambda().eq(PurchaseOrderEntity :: getisDel,"1");

        queryWrapper.eq(StringUtils.isNotBlank(req.getPurchaseId()),PurchaseOrderEntity :: getPurchaseId,req.getPurchaseId());

        int i = orderFormDao.delete(queryWrapper);
        if(i>0){
            return true;
        }else {
            return false;
        }


    }

    /**
     * 根据采购id修改
     * */
    @Override
    @Transactional(rollbackFor=Exception.class)
    public void PurchaseUpdateById(PurchaseUpdateByIdReq req) {

        LambdaUpdateWrapper<PurchaseOrderEntity> updateWrapper = new UpdateWrapper<PurchaseOrderEntity>()
                .lambda().eq(PurchaseOrderEntity :: getisDel,"1");

        updateWrapper.set(PurchaseOrderEntity :: getAddress,req.getAddress());
        updateWrapper.set(PurchaseOrderEntity :: getContactInformation,req.getContactInformation());
        updateWrapper.set(PurchaseOrderEntity :: getContacts,req.getContacts());
        updateWrapper.set(PurchaseOrderEntity :: getDistributorName,req.getDistributorName());
        updateWrapper.set(PurchaseOrderEntity :: getProductsClassification,req.getProductsClassification());
        updateWrapper.set(PurchaseOrderEntity :: getPurchaseDate,req.getPurchaseDate());
        updateWrapper.set(PurchaseOrderEntity :: getProductName,req.getProductName());
        updateWrapper.set(PurchaseOrderEntity :: getSpecification,req.getSpecification());

        updateWrapper.eq(StringUtils.isNotBlank(req.getPurchaseId()),PurchaseOrderEntity :: getPurchaseId,req.getPurchaseId());


       update(updateWrapper);

    }

    /**
     * 批量添加订单
     * */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void PurchaseAddById(List<getPurchaseAddByIdReq> req) {
        List<PurchaseOrderEntity> purchaseOrderEntities = new ArrayList<>();

        for (getPurchaseAddByIdReq getPurchaseAddByIdReq : req) {
            System.err.println(getPurchaseAddByIdReq.getDistributorName());
            PurchaseOrderEntity purchaseOrderEntity = new PurchaseOrderEntity();
            getPurchaseAddByIdReq.copyTo(purchaseOrderEntity);
            purchaseOrderEntity.setisDel("1");
            purchaseOrderEntities.add(purchaseOrderEntity);
        }

        saveBatch(purchaseOrderEntities);

    }
}